const express = require('express')
const router = express.Router();
const db = require('../../utils/db')

// 查询桌位区域
router.get('/gettablearea', function (req, res) {
    let mysql = `SELECT table_position FROM table_area GROUP BY table_position`
    db(mysql, null, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询桌位区域失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询桌位区域成功',
            data: data
        })
    })
})

// 查询桌位数据
router.get('/gettablelist', function (req, res) {
    let mysql = `SELECT * FROM table_area WHERE table_position=?`
    db(mysql, [req.query.area], function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询桌位列表失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询桌位列表成功',
            data: data
        })
    })
})

// 查询桌位编号
router.post('/gettablenum', function (req, res) {
    let mysql = 'SELECT table_num FROM table_area WHERE table_position=? GROUP BY table_num DESC LIMIT 0,1';
    db(mysql, [req.body.table_position], function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询桌位编号失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询桌位编号成功',
            data: data
        })
    })
})

// 添加桌位数据
router.post('/addtablelist', function (req, res) {
    let mysql = 'INSERT INTO table_area VALUE(NULL,?,?,?,?,?,NULL)';
    db(mysql, [req.body.table_position, req.body.table_contain, req.body.table_state, req.body.table_num, req.body.table_size], (err, data) => {
        if (err) {
            return res.json({
                code: 500,
                msg: '添加桌位失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '添加桌位成功',
            data: data
        })
    })
})

// 修改桌位数据
router.post('/updatetablelist', function (req, res) {
    let mysql = 'UPDATE table_area SET table_size=?,table_contain=?,table_position=?,table_state=? WHERE table_id=?';
    db(mysql, [req.body.table_size, req.body.table_contain, req.body.table_position, req.body.table_state, req.body.table_id], (err, data) => {
        if (err) {
            return res.json({
                code: 500,
                msg: '修改桌位失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '修改桌位成功',
            data: data
        })
    })
})

// 删除桌位数据
router.post('/deltablelist', function (req, res) {
    let mysql = 'UPDATE table_area SET table_del_state=0 WHERE table_id=?';
    db(mysql, [req.body.table_id], (err, data) => {
        if (err) {
            return res.json({
                code: 500,
                msg: '删除桌位失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '删除桌位成功',
            data: data
        })
    })
})
module.exports = router;